import requests


class EmployeeApi(object):
    @classmethod
    def add_emp(cls, headers, body):
        url_add = "http://ihrm-test.itheima.net/api/sys/user"
        return requests.post(url=url_add, headers=headers, json=body)

    @classmethod
    def query_emp(cls, user_id, headers):
        url_query = f"http://ihrm-test.itheima.net/api/sys/user" + "/" + user_id
        return requests.get(url=url_query, headers=headers)

    @classmethod
    def modify_emp(cls, user_id, headers, body):
        url_modify = f"http://ihrm-test.itheima.net/api/sys/user" + "/" + user_id
        return requests.put(url=url_modify, headers=headers, json=body)

    @classmethod
    def del_emp(cls, user_id, headers):
        url_del = f"http://ihrm-test.itheima.net/api/sys/user" + "/" + user_id
        return requests.delete(url=url_del, headers=headers)


if __name__ == '__main__':
    url_login = "http://ihrm-test.itheima.net/api/sys/login"
    headers_login = {"Content-Type": "application/json"}
    body_login = {
        "mobile": "13800000002",
        "password": "123456"
    }
    resp_login = requests.post(url=url_login, headers=headers_login, json=body_login)
    print(resp_login.json())

    token = "Bearer " + resp_login.json().get("data")
    headers = {
        "Content-Type": "application/json",
        "Authorization": token
    }
    body = {
        "username": "YG001",
        "mobile": "18210227943",
        "workNumber": "123"
    }

    data = EmployeeApi.add_emp(headers=headers, body=body)
    print(data.json())

    user_id = data.json().get("data").get("id")
    data = EmployeeApi.query_emp(headers=headers, user_id=user_id)
    print(data.json())

    # headers= headers
    body = {
        "username": "YG002",
        "password": "123456",
        "departmentId": "10086"
    }
    data = EmployeeApi.modify_emp(user_id=user_id, headers=headers, body=body)
    print(data.json())

    # headers= headers
    data = EmployeeApi.del_emp(user_id=user_id, headers=headers)
    print(data.json())
